package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class XinminNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.xinmin.cn/[0-9]*/[0-9]*/[0-9]*/[0-9]*.html",
				"http://.*[.]xinmin[.]cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[.]html"
				,"http://.*[.]xinmin[.]cn/news/story_[0-9]*[.]html"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if(!isDetailPage(urlMeta.getUrl())){
		}
		String html=urlMeta.getHtml();
		if ( html== null) {
		}
		String title="";
		String content="";;
		Long date=null;
		int commentNum=0;
		int clickNum=0;
		String url=urlMeta.getUrl();
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title= doc.select("h1.article_title").text();
		if(StringUtils.isBlank(title)){
			title= doc.select("h1#MP_title").text();
		}
		if(StringUtils.isBlank(title)){
			title= doc.select("h1#title").text();
		}
		if(StringUtils.isBlank(title)){
			title= doc.select("div.cs_title>h1").text();
		}
		if(StringUtils.isBlank(title)){
			title= doc.select("div.pagecontent div#newseries.newseries p.effect_tit").text();
		}
		if(StringUtils.isBlank(title)){
			title= doc.select("title").text();
		}
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate( doc.select("div.MP_datetime").text());
		if(date==null){
			date=DateUtils.matchDate( doc.select("span#time").text());
		}
		if(date==null){
			try {
				date=DateUtils.matchDate( doc.select("div.cs_title>p>em").get(1).text());
			} catch (Exception e) {
			}
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.cs_title").html());
		}
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#MP_article p "));
		if (content!=null && !doc.select("div#MP_article p:containsOwn(　　延伸阅读)").isEmpty()){
			content=StringUtils.substringBefore(content, "　　延伸阅读");
		}

		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.cs_content"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.a_p"));
		}
		if(date==null || date==0){
			date=DateUtils.matchDate(url);
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		String author=null;
		author =JSoupUtils.matchAuthor(doc, "来源：");
		meta.setAuthor(author);
		return meta;
	}

	public static void main(String[] args) {

		String url="http://shanghai.xinmin.cn/msrx/2016/09/08/30411234.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		XinminNewsAnalyse cnhubeiNewsAnalyse=new XinminNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
		System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}

}
